package cn.sumpu.app.wlcity.web.mvc.hotspring;


import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.wlcity.biz.SeckillBiz;
import cn.sumpu.app.wlcity.domain.Code;
import cn.sumpu.app.wlcity.domain.Seckill;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;

@Controller
public class TryLuckController {
	
	@Autowired
	SeckillBiz seckillBiz;
	//加载试试手气列表页面
	@RequestMapping(value = "/tryLuck.htm")
	public String tryLuck(Integer page,String seckillType,HttpServletRequest request) {
		if(page == null){
			page = 1;
		} 
		if(seckillType == null){
			seckillType = "02";
		} 
		try {
			List<Seckill> seckillListTry = seckillBiz.getSeckillListFive(page,seckillType); 
			int size = seckillListTry.size();
			int totalPage = (size + Common.PAGE_SIZE_SECKILLFIVE- 1) / Common.PAGE_SIZE_SECKILLFIVE;
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("seckillListTry", seckillListTry);
			request.setAttribute("link", "hot.htm");
			request.setAttribute("currentPage", page);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/tryLuck/tryLuck";
	}
	//试手气规则
	@RequestMapping(value = "/tryLuckRule.htm")
	public String tryLuckRule(HttpServletRequest request) {
		try {
			Code code = seckillBiz.getSkillTry();
			request.setAttribute("code", code);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "hotspring/tryLuck/tryLuckRule";
	}
	
	//试手气规则管理页面
	@RequestMapping(value = "/tryRule.cms")
	public String tryRule(HttpServletRequest request) {
		try {
			Code code = seckillBiz.getSkillTry();
			request.setAttribute("code", code);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "cms/seckill/tryRule";
	}
	
	
	
	//保存手气规则
	@RequestMapping(value = "/saveTryRule.cms")
	public String saveTryRule(String content,HttpServletRequest request,HttpServletResponse response) {
		try {
			
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();  
			seckillBiz.updateSkillTry(content);
			pw.write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/*//查询试手气的详细页面
	@RequestMapping(value = "/tryLuckTry.htm")
	public String tryLuckTry(String seckillId,HttpServletRequest request) {
		try { 
			Boolean isOver =seckillBiz.isSeckillOver(seckillId);
			request.setAttribute("isOver", isOver);
			Seckill tryLuck =seckillBiz.selectSeckill(seckillId); 
			request.setAttribute("tryLuck", tryLuck);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/tryLuck/tryLuckChildren";
	}
    */	
	//试手气数据传送页面
	@RequestMapping(value = "/tryLuckPhone.htm")
	public String tryLuckPhone(String phoneNum,String seckillId,HttpServletRequest request) {
		
		try { 
			if(phoneNum==null||phoneNum==""){
				request.setAttribute("value", "phoneError");
				Seckill tryLuck =seckillBiz.selectSeckill(seckillId); 
				request.setAttribute("tryLuck", tryLuck);
			}
			else{ 
			String value= seckillBiz.seckill(seckillId,phoneNum);
			request.setAttribute("value", value);
			Seckill tryLuck =seckillBiz.selectSeckill(seckillId); 
			request.setAttribute("tryLuck", tryLuck);
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/tryLuck/trySuccess";
		
		
		
		
	}
	//接受移动传送数据返回秒杀页面
	@RequestMapping(value = "/trySeckill.htm")
	public String tryLuckTry(String seckillId,String seckillType,String phoneNumber,HttpServletRequest request) {
		//试手气
		if(seckillType!=null && seckillType.equals("luck")){
		
		try { 
			Boolean isOver =seckillBiz.isSeckillOver(seckillId);
			request.setAttribute("isOver", isOver);
			Seckill tryLuck =seckillBiz.selectSeckill(seckillId); 
			request.setAttribute("tryLuck", tryLuck);
			request.setAttribute("phoneNumber", phoneNumber);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/tryLuck/tryLuckChildren";
		}
		//精彩活动
		else if(seckillType!=null && seckillType.equals("snatch")){
			try { 
				Seckill seckill =seckillBiz.selectSeckill(seckillId); 
				request.setAttribute("seckill", seckill);
				Boolean isOver =seckillBiz.isSeckillOver(seckillId); 
				request.setAttribute("isOver", isOver);
				request.setAttribute("phoneNumber", phoneNumber);
			} catch (AppBusinessException e) {
				e.printStackTrace();
				return "hotspring/error";
			}
			return "hotspring/seckill/seckillChildren";
		}
		
		return null;
	}
	
	
	
}
